package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentPriceAdjustmentBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.PriceAdjustmentBean;
import com.sgb.member.ui.adapter.PriceAdjustmentAdapter;
import com.sgb.member.view_model.PriceAdjustmentModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class PriceAdjustmentFragment extends AppLazyFragment implements MemberStatusView.OnChooseListener {

    @Inject
    PriceAdjustmentModel sModel;
    private FragmentPriceAdjustmentBinding mBinding;
    private PriceAdjustmentAdapter mAdapter;
    private int mCurPos;
    private String mCurTitle;
    // 数据集
    private final SparseArray<List<CommonPopEntity>> mData = new SparseArray<>();
    private final SparseIntArray mFilter = new SparseIntArray();
    private CommonPopEntity mAll;
    private String mNo1;
    private String mNo2;
    private String mNo3;
    private String mNo4;

    public static PriceAdjustmentFragment newInstance(int position, String title) {
        PriceAdjustmentFragment fragment = new PriceAdjustmentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putString("TITLE", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_price_adjustment, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
        mCurTitle = getArguments().getString("TITLE", "");
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
        initRecycler();
        initPopup();
    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
        loadOtherData();
    }

    public void loadData(boolean isLoadMore) {
        sModel.getPriceAdjustment(mCurPos, isLoadMore, mNo1, mNo2, mNo3, mNo4);
    }

    private void loadOtherData() {
        switch (mCurPos) {
            case 0:
                sModel.getAllCategory(PickCateType.TYPE_1, false); // 行业分类
                break;
            case 1:
                sModel.getAllLeaseType(PickCateType.TYPE_1, false); // 行业分类
                break;
            case 2:
                sModel.getAllTeamType(PickCateType.TYPE_1, false); // 行业分类
                break;
        }
    }

    /**
     * -------------------------------------initRecycler------------------------------------------
     */
    private void initRecycler() {
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        mAdapter = new PriceAdjustmentAdapter(getActivity(), null, mCurPos, mCurTitle);
        mBinding.recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<PriceAdjustmentBean>() {
            @Override
            public void onItemClick(PriceAdjustmentBean bean, int position) {
                sModel.getUrlSetting(mCurPos, bean);

            }
        });
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void showEmpty() {
        mBinding.recyclerView.setVisibility(View.GONE);
        mBinding.empView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<PriceAdjustmentBean> list, boolean isLoadMore, boolean noMoreData) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setLoadingMoreEnabled(!noMoreData);
        closeRecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.empView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.empView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * ------------------------------- Popup ---------------------------------------------
     */
    private void initPopup() {
        mBinding.popup1.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup2.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup3.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup4.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup2.setVisibility(View.GONE);
        mBinding.popup3.setVisibility(View.GONE);
        // 劳务调价-工种名称显示
        mBinding.popup4.setVisibility(mCurPos == 2 ? View.VISIBLE : View.GONE);
        switch (mCurPos) {
            case 0: // 商品调价
                mBinding.popup1.setKeyName("分类");
                mBinding.popup2.setKeyName("类别");
                mBinding.popup3.setKeyName("品名");
                break;
            case 1: // 租赁调价
                mBinding.popup1.setKeyName("类目");
                mBinding.popup2.setKeyName("分类");
                mBinding.popup3.setKeyName("类型");
                break;
            case 2: // 劳务调价
                mBinding.popup1.setKeyName("工程分类");
                mBinding.popup2.setKeyName("工程专项");
                mBinding.popup3.setKeyName("专业分类");
                mBinding.popup4.setKeyName("工种名称");
                break;
        }
    }

    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mCurPos) {
            case 0: // 商品调价
                switch (mKeyName) {
                    case "分类":
                        sModel.getAllCategory(PickCateType.TYPE_1, true);
                        break;
                    case "类别":
                    case "品名":
                        break;
                }
                break;
            case 1: // 租赁调价
                switch (mKeyName) {
                    case "类目":
                        sModel.getAllLeaseType(PickCateType.TYPE_1, true);
                        break;
                    case "分类":
                        break;
                    case "类型":
                        break;
                }
                break;
            case 2: // 劳务调价
                switch (mKeyName) {
                    case "工程分类":
                        sModel.getAllTeamType(PickCateType.TYPE_1, true);
                        break;
                    case "工程专项":
                    case "专业分类":
                        break;
                    case "工种名称":
                        sModel.getWorkType(PickCateType.TYPE_4, true);
                        break;
                }
                break;
        }
    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        String id = position == 0 ? "" : entity.getId();
        switch (mCurPos) {
            case 0: // 商品调价
                switch (mKeyName) {
                    case "分类":
                        if (!TextUtils.equals(id, mNo1)) {
                            mNo1 = id;
                            updateBusinessOtherData(PickCateType.TYPE_1, position);
                            loadData(false);
                        }
                        break;
                    case "类别":
                        if (!TextUtils.equals(id, mNo2)) {
                            mNo2 = id;
                            updateBusinessOtherData(PickCateType.TYPE_2, position);
                            loadData(false);
                        }
                        break;
                    case "品名":
                        if (!TextUtils.equals(id, mNo3)) {
                            mNo3 = id;
                            updateBusinessOtherData(PickCateType.TYPE_3, position);
                            loadData(false);
                        }
                        break;
                }
                break;
            case 1: // 租赁调价
                switch (mKeyName) {
                    case "类目":
                        if (!TextUtils.equals(no, mNo1)) {
                            mNo1 = no;
                            updateBusinessOtherData(PickCateType.TYPE_1, position);
                            loadData(false);
                        }
                        break;
                    case "分类":
                        if (!TextUtils.equals(no, mNo2)) {
                            mNo2 = no;
                            updateBusinessOtherData(PickCateType.TYPE_2, position);
                            loadData(false);
                        }
                        break;
                    case "类型":
                        if (!TextUtils.equals(no, mNo3)) {
                            mNo3 = no;
                            updateBusinessOtherData(PickCateType.TYPE_3, position);
                            loadData(false);
                        }
                        break;
                }
                break;
            case 2: // 劳务调价
                switch (mKeyName) {
                    case "工程分类":
                        if (!TextUtils.equals(no, mNo1)) {
                            mNo1 = no;
                            updateBusinessOtherData(PickCateType.TYPE_1, position);
                            loadData(false);
                        }
                        break;
                    case "工程专项":
                        if (!TextUtils.equals(no, mNo2)) {
                            mNo2 = no;
                            updateBusinessOtherData(PickCateType.TYPE_2, position);
                            loadData(false);
                        }
                        break;
                    case "专业分类":
                        if (!TextUtils.equals(no, mNo3)) {
                            mNo3 = no;
                            updateBusinessOtherData(PickCateType.TYPE_3, position);
                            loadData(false);
                        }
                        break;
                    case "工种名称":
                        if (!TextUtils.equals(no, mNo4)) {
                            mNo4 = no;
                            loadData(false);
                        }
                        break;
                }
                break;
        }
    }

    public void setChooseItemData(int type, List<CommonPopEntity> data, boolean isShowPopup) {
        if (data != null && data.size() > 0) {
            String name = data.get(0).getTypeName();
            if (!TextUtils.isEmpty(name) && (!TextUtils.equals("全部", name) && !TextUtils.equals("不限", name))) {
                data.add(0, mAll);
            }
        }
        addMainEntity(type, data);
        switch (type) {
            case PickCateType.TYPE_1:
                mBinding.popup1.setData(data, mNo1);
                if (isShowPopup) {
                    mBinding.popup1.showPop();
                }
                break;
            case PickCateType.TYPE_2:
                mBinding.popup2.setData(data, mNo2);
                if (isShowPopup) {
                    mBinding.popup2.showPop();
                }
                break;
            case PickCateType.TYPE_3:
                mBinding.popup3.setData(data, mNo3);
                if (isShowPopup) {
                    mBinding.popup3.showPop();
                }
                break;
            case PickCateType.TYPE_4:
                mBinding.popup4.setData(data, mNo4);
                if (isShowPopup) {
                    mBinding.popup4.showPop();
                }
                break;
        }
    }

    public void addMainEntity(int index, List<CommonPopEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).clear();
            } else {
                mData.put(index, list);
            }
        } else {
            mData.put(index, list);
        }
        mFilter.put(index, 0);
    }

    private void updateBusinessOtherData(int index, int position) {
        mFilter.put(index, position);
        switch (index) {
            case PickCateType.TYPE_1:
                mNo2 = "";
                mNo3 = "";
                mBinding.popup2.setVisibility(View.GONE);
                mBinding.popup3.setVisibility(View.GONE);
                mBinding.popup2.refreshChoose(mNo2, "", 0);
                mBinding.popup3.refreshChoose(mNo3, "", 0);
                setChooseItemData(PickCateType.TYPE_2, new ArrayList<>(), false);
                setChooseItemData(PickCateType.TYPE_3, new ArrayList<>(), false);
                if (position > 0) {
                    CommonPopEntity popEntity = mData.get(index).get(position);
                    if (popEntity.getChildren() != null && popEntity.getChildren().size() > 0) {
                        List<CommonPopEntity> list = new ArrayList<>(popEntity.getChildren());
                        setChooseItemData(PickCateType.TYPE_2, list, false);
                        mBinding.popup2.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case PickCateType.TYPE_2:
                mNo3 = "";
                mBinding.popup3.setVisibility(View.GONE);
                mBinding.popup3.refreshChoose(mNo3, "", 0);
                setChooseItemData(PickCateType.TYPE_3, new ArrayList<>(), false);
                if (position > 0) {
                    CommonPopEntity popEntity = mData.get(index).get(position);
                    if (popEntity.getChildren() != null && popEntity.getChildren().size() > 0) {
                        List<CommonPopEntity> list = new ArrayList<>(popEntity.getChildren());
                        setChooseItemData(PickCateType.TYPE_3, list, false);
                        mBinding.popup3.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }

    }
}
